package com.lagou.homework122;

/**
 * 裁判类
 * 判断五子棋输赢
 */
public class Referee {

    public static int BOARD_SIZE = 17;

    /*
     * 四个方向，其中之一满足则代表出现赢棋，返回true，否则返回false
     */
    public static boolean check(int[][] board, final int x, final int y) {
        return checkColumn(board, x, y) || checkRow(board, x, y)
                || checkLeftLea(board, x, y) || checkRightLea(board, x, y);
    }

    /*
     *	纵向查找的方法
     *	分为上下两个部分
     *	其他方向上同理
     */
    private static boolean checkColumn(int[][] board, int x, int y) {
        int xCount = 1;
        /*
         *	向上查找
         *	y - i >= 0 是为了防止越界的条件
         *	board[x][y - i] == board[x][y] 由中心向外判断棋子是否相连的条件
         */
        for (int i = 1; i <= 4; i++) {
            if (y - i >= 0 && board[x][y - i] == board[x][y]) {
                xCount++;    //如果相连，该方向上的棋子数加一
            } else {
                break;    //如果遇到第一个空位或非己方棋子退出循环
            }
        }
        //向下查找
        //y + i < BOARD_SIZE 同样为了防止越界
        for (int i = 1; i <= 4; i++) {
            if (y + i < BOARD_SIZE && board[x][y + i]
                    == board[x][y]) {
                xCount++;
            } else {
                break;
            }
        }
        //如果上下两个方向上相连的棋子相加大于等于5，返回true，否则返回false
        return xCount >= 5;
    }

    private static boolean checkRow(int[][] board, int x, int y) {
        int yCount = 1;
        //向左查找
        for (int i = 1; i <= 4; i++) {
            if (x - i >= 0 && board[x - i][y] == board[x][y]) {
                yCount++;
            } else {
                break;
            }
        }
        //向右查找
        for (int i = 1; i <= 4; i++) {
            if (x + i < BOARD_SIZE &&
                    board[x + i][y] == board[x][y]) {
                yCount++;
            } else {
                break;
            }
        }
        return yCount >= 5;
    }

    private static boolean checkLeftLea(int[][] board, int x, int y) {
        int leftLeaCount = 1;
        //向左上查找
        for (int i = 1; i <= 4; i++) {
            if (x - i >= 0 && y - i >= 0 && board[x - i][y - i]
                    == board[x][y]) {
                leftLeaCount++;
            } else {
                break;
            }
        }
        //向右下查找
        for (int i = 1; i <= 4; i++) {
            if (x + i < BOARD_SIZE && y + i < BOARD_SIZE &&
                    board[x + i][y + i] == board[x][y]) {
                leftLeaCount++;
            } else {
                break;
            }
        }
        return leftLeaCount >= 5;
    }

    private static boolean checkRightLea(int[][] board, int x, int y) {
        int rightLeaCount = 1;
        //向左下查找
        for (int i = 1; i <= 4; i++) {
            if (x + i < BOARD_SIZE && y - i >= 0 &&
                    board[x + i][y - i] == board[x][y]) {
                rightLeaCount++;
            } else {
                break;
            }
        }
        //向右上查找
        for (int i = 1; i <= 4; i++) {
            if (x - i >= 0 && y + i < BOARD_SIZE &&
                    board[x - i][y + i] == board[x][y]) {
                rightLeaCount++;
            } else {
                break;
            }
        }
        return rightLeaCount >= 5;
    }

}